package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.common.util.FileUtils;
import cc.mrbird.web.domain.OneStudentTemp;
import cc.mrbird.web.domain.RespImport;
import cc.mrbird.web.domain.VisitStudentTemp;
import cc.mrbird.web.service.VisitStudentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;


@Controller
public class VisitExcelImportController {

    private static final Logger log = LoggerFactory.getLogger(VisitExcelImportController.class);

    @Autowired
    private VisitStudentService visitStudentService;

    @Log("导入考生信息")
    @RequestMapping("visit_import")
    @RequiresPermissions("visit_import:add")
    public String imports() {
        return "web/visit_import/visit_import";
    }


    @PostMapping("/visitStudentImport")
    @ResponseBody
    public RespImport studentImport(@RequestParam("file") MultipartFile file, String username) {
        log.info("startTime:" + System.currentTimeMillis());
        RespImport respImport = visitStudentService.ImportVisitStudent(file,username);
        log.info("endTime:" + System.currentTimeMillis());
        return respImport;
    }

    @RequestMapping("/visitDownloadTemplate")
    @ResponseBody
    public ResponseBo visitTemplateExcel() {
        VisitStudentTemp student1 = new VisitStudentTemp();
        student1.setGraduateHigh("信阳一高（必填）");
        student1.setName("张三（必填）");
        student1.setStatus("老师（必填）");
        student1.setTelephone("15515722012（必填）");
        student1.setNote("备注，允许为空");

        VisitStudentTemp student = new VisitStudentTemp();
        student.setGraduateHigh("商城一高（必填）");
        student.setName("李四（必填）");
        student.setStatus("学生（必填）");
        student.setTelephone("15515733000（必填）");
        student.setNote("备注，允许为空");

        ArrayList<VisitStudentTemp> list = new ArrayList<>();
        list.add(student);
        list.add(student1);
        try {
            return FileUtils.createExcelByPOIKit("来校师生参观信息模板", list, VisitStudentTemp.class);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }
}